dojo.provide("uws.drawing.classdiagram.ClassBox");
dojo.require('uws.drawing.Component');

dojo.declare('uws.drawing.classdiagram.ClassBox',uws.drawing.Component,{
	constructor: function(surface,id,dx,dy, sortOrder,label,fromDS){
		this.center_x = 100;
        this.center_y = 80;
		this.sortOrder = sortOrder;
		
		if(fromDS){
			dx+=this.center_x;
			dy+=this.center_y;
		}
		
		//connectors
		this.top_con=this.connectors_shape_group.createCircle({
			cx: this.center_x + 5,
			cy: 5,
			r:3
		}).setStroke("white").setFill("white");
		
		this.bottom_con=this.connectors_shape_group.createCircle({
			cx: this.center_x + 5,
			cy: (this.center_y*2) +5,
			r:3
		}).setStroke("white").setFill("white");
		
		this.left_con=this.connectors_shape_group.createCircle({
			cx: 5,
			cy: this.center_y,
			r:3
		}).setStroke("white").setFill("white");
		
		this.right_con=this.connectors_shape_group.createCircle({
			cx: (this.center_x*2) + 5,
			cy: this.center_y,
			r:3
		}).setStroke("white").setFill("white");
		
		//invi for moveable
		this.actual_shape_group.createRect({
			x:5,
			y:5,
			width:200,
			height:160
		}).setStroke("white").setFill("white");
		
		this.actual_shape_group.createRect({
			x:5,
			y:5,
			width:200,
			height:20
		}).setStroke("black");
		
		this.actual_shape_group.createRect({
			x:5,
			y:25,
			width:200,
			height:70
		}).setStroke("black");
		
		this.actual_shape_group.createRect({
			x:5,
			y:95,
			width:200,
			height:70
		}).setStroke("black");
		
		//classname
		this.label_shape = this.group.createText({
			text: label,
			x: this.center_x,
			y: (this.center_y - 60),
			align:"middle"
		}).setFill("black").setFont(defaultFont);
		
		//variables
		this.label_shape2 = this.actual_shape_group.createGroup();
		
		var txtArgs = {
			text: "",
			x: 10,
			y: (this.center_y- 50),
			width: 190,
			align: "start", 
			fitting: dojox.gfx.vectorFontFitting.FLOW,
			leading: 1,
			trailing: 1
		};
		
		//methods
		this.label_shape3 = this.actual_shape_group.createGroup();
		var txtArgs2 = {
			text: "",
			x: 10,
			y: (this.center_y + 20),
			width: 190,
			align: "start", 
			fitting: dojox.gfx.vectorFontFitting.FLOW,
			leading: 1,
			trailing: 1
		};
		
		var fontArgs = {
			size: "10pt", family: "Gillius"
		};
		
		try{
			vf.draw(this.label_shape2, txtArgs, fontArgs, "black");
			vf.draw(this.label_shape3, txtArgs2, fontArgs, "black");
		}catch(e){
			console.debug("Catched IE bugs. Darn you, IE.");
		};
		
		this._postCreate(dx, dy);
		this.drawChildren();
	},
	applyProperties:function(properties){
		var fontArgs = {
			size: "10pt", family: "Gillius"
		};
		
		//classname
		this.label_shape.setShape({
			text: properties.label,
			x: this.center_x,
			y: (this.center_y - 60),
			align:"middle"
		}).setFill("black").setFont(defaultFont);
	},
	drawChildren:function(){
		dataStore.fetchItemByIdentity({
			identity: this.id,
			scope:this,
			onItem:function(item){
				var children = dataStore.getValues(item, "comp_children");
				var functions = "";
				var variables = "";
				
				for(var i=0; i<children.length; i++){
					if(dataStore.getValue(children[i], "type")=="variable"){
						var temp = "";
						
						if(dataStore.getValue(children[i], "modifier")=="private"){
							temp+="- ";
						}else if(dataStore.getValue(children[i], "modifier")=="protected"){
							temp+="# ";
						}else if(dataStore.getValue(children[i], "modifier")=="public"){
							temp+="+ ";
						}else{
							temp+="  ";
						}
						
						temp += dataStore.getValue(children[i], "name");
						temp += ": ";
						temp += dataStore.getValue(children[i], "childType");
						
						variables += temp+"\n";
					}else if(dataStore.getValue(children[i], "type")=="function"){
						var temp = "";
						
						if(dataStore.getValue(children[i], "modifier")=="private"){
							temp+="- ";
						}else if(dataStore.getValue(children[i], "modifier")=="protected"){
							temp+="# ";
						}else if(dataStore.getValue(children[i], "modifier")=="public"){
							temp+="+ ";
						}else{
							temp+="  ";
						}
						
						temp += dataStore.getValue(children[i], "name");
						temp += "(" + dataStore.getValue(children[i], "parameters") + "): ";
						temp += dataStore.getValue(children[i], "childType");
						
						functions += temp + "\n";
					}
				}
				
				var fontArgs = {
					size: "10pt", family: "Gillius"
				};
				
				//variables
				
				var txtArgs = {
					text: variables,
					x: 10,
					y: (this.center_y- 50),
					width: 190,
					align: "start", 
					fitting: dojox.gfx.vectorFontFitting.FLOW,
					leading: 1,
					trailing: 1
				};
				
				//methods
				var txtArgs2 = {
					text: functions,
					x: 10,
					y: (this.center_y + 20),
					width: 190,
					align: "start", 
					fitting: dojox.gfx.vectorFontFitting.FLOW,
					leading: 1,
					trailing: 1
				};
				
				this.label_shape2.removeShape();
				this.label_shape3.removeShape();
				
				this.label_shape2 = this.actual_shape_group.createGroup();
				this.label_shape3 = this.actual_shape_group.createGroup();
				try{
					vf.draw(this.label_shape2, txtArgs, fontArgs, "black");
					vf.draw(this.label_shape3, txtArgs2, fontArgs, "black");
				}catch(e){
					console.debug("Catched IE bugs. Darn you, IE.");
					console.dir(e);
				};
			}
		});
	}
});